package cn.java71.servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
@WebFilter(filterName = "mytimer",value = "/*")
public class MyTimeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入了时间的过滤器");
        long start=System.nanoTime();
        //请求地址
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        request.setCharacterEncoding("utf-8");
        //String con=request.getContextPath();

        String path=request.getServletPath();
        System.out.println("进入了timerFilter ：当前请求地址："+path);
        //放行
        filterChain.doFilter(request,servletResponse);

        long end=System.nanoTime();
        System.out.println("当前请求地址："+path+"花费了"+(end-start)+"纳秒");

        System.out.println("离开了时间的过滤器");
    }

    @Override
    public void destroy() {

    }
}
